<template>
  <div class="pager-select" :class="'pager_select_'+size">
    <input
      class="el-input__inner"
      :value="value"
      v-bind="$attrs"
      :class="'pager_select_'+size"
      type="text"
      autocomplete="off"
      :placeholder="placeholder"
      readonly
      @input="$emit('change', $event.target.value)"
      @focus="handleFocus"
    >
    <span class="el-input__suffix">
      <span class="el-input__suffix-inner">
        <i class="custom-i" :class="['el-select__caret', 'el-input__icon', 'el-icon-' + iconClass]" />
      </span>
    </span>
    <div ref="input" class="custom-select-tag">
      <span v-for="item in selectObject" :key="item" class="span-tag">{{ item }}<i
        class="custom-icon-close el-icon-close"
        @click="deleteObject(item)"
      /></span>
    </div>
    <div v-if="isShow">
      <div class="custom-popper__arrow" />
      <div ref="show" class="custom-tag-body" style="overflow: hidden">
        <div class="scroll-body">
          <el-table
            ref="multipleTable"
            v-loading="loading"
            :data="dataList"
            style="width: 100%"
            height="220"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="55"
              :selectable="customSelectTable"
            />
            <el-table-column prop="title" label="标题" />
            <el-table-column>
              <template slot="header" slot-scope="scope">
                <el-input
                  v-model="query.blurry"
                  size="mini"
                  placeholder="请输入查询的内容"
                >
                  <i slot="suffix" class="el-input__icon el-icon-search" @click="doQuery(scope)" />
                </el-input>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page="query.page"
            :page-size="query.size"
            :total="pagerSize"
            style="margin-top: 8px;"
            layout="total, prev, pager, next, sizes"
            :page-sizes="[5, 10, 15, 20]"
            @current-change="pagerHandleCurrentChange"
            @size-change="pagerHandleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { queryAllConsult } from '@/api/consult'

export default {
  name: 'CustomPageIndex',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: [String, Number],
    size: {
      type: String,
      default: 'small'
    },
    placeholder: String,
    clearable: {
      type: Boolean,
      default: false
    },
    id: [String, Number],
    length: [String, Number]
  },
  data() {
    return {
      visible: false,
      isShow: false,
      pagerSize: 0,
      dataList: [],
      query: {
        blurry: '',
        page: 1,
        size: 5,
        id: this.id
      },
      loading: false,
      selectObject: [],
      selects: []
    }
  },
  computed: {
    iconClass() {
      return this.visible === true ? 'arrow-up is-reverse' : 'arrow-down'
    }
  },
  watch: {
    id: {
      handler(newVal, oldVal) {
        this.query.id = newVal
        this.initData()
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    selectObject: {
      handler(newVal, oldVal) {
        if (undefined !== newVal && newVal !== null && newVal.length > 0) {
          this.$emit('change', newVal.toString())
        } else {
          this.$emit('change', '')
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    document.addEventListener('click', e => {
      const input = this.$refs.input
      const show = this.$refs.show
      if ((undefined !== input || undefined !== show) && (input.contains(e.target) ||
        (this.isShow === true && show.contains(e.target)))) {
        this.visible = true
        this.isShow = true
      } else {
        this.visible = false
        this.isShow = false
      }
      if (undefined !== this.value && this.value !== null && this.value !== '') {
        this.selectObject = this.value.split(',')
      } else {
        this.selectObject = []
      }
    })
    this.initData()
  },
  methods: {
    handleFocus() {
      this.visible = true
      this.isShow = true
    },
    doQuery() {
      this.initData()
    },
    initData() {
      this.loading = true
      queryAllConsult(this.query).then(res => {
        this.loading = false
        const { data, pagerSize } = res
        this.pagerSize = pagerSize
        if (undefined !== data && data !== null) {
          this.dataList = data
        } else {
          this.dataList = []
        }
      }).catch(() => {
        this.loading = false
      })
    },
    pagerHandleCurrentChange(val) {
      this.query.page = val
      this.initData()
    },
    pagerHandleSizeChange(val) {
      this.query.size = val
      this.initData()
    },
    customSelectTable(row) {
      if (this.selectObject.length <= this.length) {
        return true
      } else {
        return false
      }
    },
    handleSelectionChange(val) {
      this.selectObject = val.map(item => {
        return item.title
      })
    },
    deleteObject(object) {
      if (this.selectObject.length > 0) {
        this.selectObject = this.selectObject.filter(item => {
          if (item !== object) {
            return item
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.pager-select {
  width: 100%;
  cursor: pointer;
  position: relative;
}

.el-input__inner {
  outline: none;
  border: 1px solid #dcdfe6;
  transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
  border-radius: 4px;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  padding: 0px 15px;
  box-sizing: border-box;
}

.pager_select_suffix {
  height: 40px;
  line-height: 40px;
  font-size: inherit;
}

.pager_select_medium {
  height: 36px;
  line-height: 36px;
  font-size: inherit;
}

.pager_select_small {
  height: 32px;
  line-height: 32px;
  font-size: inherit;
}

.pager_select_mini {
  height: 28px;
  line-height: 28px;
  font-size: inherit;
}

.el-input__inner:focus {
  outline: none;
  border-color: #409eff;
}

.pager_select_suffix .custom-i {
  line-height: 40px;
}

.pager_select_medium .custom-i {
  line-height: 36px;
}

.pager_select_small .custom-i {
  line-height: 32px;
}

.pager_select_mini .custom-i {
  line-height: 28px;
}

.custom-i {
  color: #c0c4cc;
  font-size: 14px;
  transition: transform .3s;
  transform: rotate(180deg);
  cursor: pointer;
}

.custom-tag-body {
  position: absolute;
  z-index: 9998;
  left: 0px;
  right: 0px;
  height: 280px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 10px 15px;
  box-sizing: border-box;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  margin-top: 8px;
}

.custom-popper__arrow {
  position: absolute;
  z-index: 9999;
  height: 10px;
  width: 10px;
  left: 35px;
  background-color: #ffffff;
  transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  margin-top: 4px;
  border-radius: 1px;
  border-left: 1px solid #dcdfe6;
  border-top: 1px solid #dcdfe6;
}

.custom-select-tag {
  position: absolute;
  left: 0px;
  right: 30px;
  bottom: 0px;
  top: 0px;
  z-index: 1;
}

.span-tag {
  padding: 5px 10px;
  border-radius: 2px;
  border: 1px solid #e9e9eb;
  background-color: #f4f4f5;
  color: #909399;
  margin-left: 5px;
  font-size: 12px;
}

.custom-icon-close {
  border-radius: 50%;
  width: 14px;
  height: 14px;
  line-height: 14px;
  text-align: center;
  margin-left: 5px;
  font-size: 12px;
  color: #909399;
  background-color: #c0c4cc;
}
</style>
